package com.wiz.web.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.google.gson.reflect.TypeToken;
import com.wiz.web.dao.mapper.HqStationMapper;
import com.wiz.web.manager.WxManager;
import com.wiz.web.service.AccountService;
import com.wiz.web.service.HqStationService;
import com.wiz.web.vo.AdminGroupHistoryVO;
import com.wiz.web.vo.SyncResultVO;
import com.wiz.web.vo.WxModelMessageVO;
import com.wiz.web.vo.criteria.FpCriteria;
import com.wiz.web.vo.criteria.HqStationCriteria;
import com.wiz.web.vo.to.AdminGroupFpOrderHistoryTO;
import com.wiz.web.vo.to.AdminTO;
import com.wiz.web.vo.to.FpTO;
import com.wiz.web.vo.to.MemberGradeTO;
import com.wiz.web.vo.to.OilTankTO;
import com.wiz.web.vo.to.PageTO;
import com.wiz.web.vo.to.PointTO;
import com.wiz.web.vo.to.ResultTO;
import com.wiz.web.vo.to.StationTO;

@SuppressWarnings("all")
@Service("hqStationService")
public class HqStationServiceImpl extends BasicServiceImpl implements HqStationService {
	private transient final Logger log = LoggerFactory.getLogger(getClass());

	@Resource
	private HqStationMapper hqStationMapper;
	@Resource
	private AccountService accountService;
	@Resource
	private WxManager wxManager;
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
	public String getGradesByHqId(HqStationCriteria criteria) {
		List<MemberGradeTO> list = hqStationMapper.getGradesByHqId(criteria);
		PageTO page = new PageTO();
		page.setPageNo(criteria.getPageNo());
		page.setPageSize(criteria.getPageSize());
		if(criteria.getPageSize()!=null){
			int total = hqStationMapper.getCntGradesByHqId(criteria);
			page.setPageCount(totalPage(total,criteria.getPageSize()));
			page.setTotal(total);
		}
		page.setDataList(list);
		return returnResult(CODE_OK, STRING_OK, page);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
	public String getPointList(HqStationCriteria criteria) {
		List<PointTO> list = hqStationMapper.getPointsByHqId(criteria);
		PageTO page = new PageTO();
		page.setPageNo(criteria.getPageNo());
		page.setPageSize(criteria.getPageSize());
		if(criteria.getPageSize()!=null){
			int total = hqStationMapper.getCntPointsByHqId(criteria);
			page.setPageCount(totalPage(total,criteria.getPageSize()));
			page.setTotal(total);
		}
		page.setDataList(list);
		return returnResult(CODE_OK, STRING_OK, page);
	}

	@Override
	public String exchangePoint(HqStationCriteria criteria) throws Exception{
		WxModelMessageVO vo = accountService.exchangePoint(criteria);
		if(vo != null){
			wxManager.exchangePointMessage(vo, criteria.getMemberId());
			return RESPONSE_CODE_OK;
		}
		return RESPONSE_CODE_INVALID_MEMBER;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
	public String insertAdminGroup(List<AdminTO> adminList) throws Exception {
		for(AdminTO to:adminList){
			if(to.getGroupStartTime() != null){
				if(hqStationMapper.selectAdminGroup(to) < 1){
					hqStationMapper.insertStartAdminGroup(to);
				}
			}else if(to.getGroupEndTime() != null){
				hqStationMapper.updateEndAdminGroup(to);
			}
		}
		return RESPONSE_CODE_OK;
	}
	
	@Override
	public String insertAttendHistory(AdminTO to) {
		int result = hqStationMapper.insertAttendHistory(to);
		if(result>0){
			return RESPONSE_CODE_OK;
		}else{
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
	public String configFpsByStation(List<FpCriteria> criteriaList) throws Exception {
		List<SyncResultVO> voList = new ArrayList<SyncResultVO>();
		for(FpCriteria criteria:criteriaList){
			int result = 0;
			SyncResultVO resultVo = new SyncResultVO();
			resultVo.setFpNo(criteria.getFpNo());
			resultVo.setFpId(criteria.getFpId());
			FpTO qr = null;
			if(StringUtils.isNotBlank(criteria.getQrId())){
				qr = hqStationMapper.getFpQrById(criteria);
				if(qr != null){
					hqStationMapper.deleteFpQr(qr);
				}
				result = hqStationMapper.updateFpQr(criteria);
				resultVo.setQrStatus(1);
			}else{
				resultVo.setQrStatus(0);
			}
			FpTO fp = hqStationMapper.getFpById(criteria);
			if(fp == null){
				result = hqStationMapper.insertFpByStation(criteria);
			}else{
				result = hqStationMapper.updateFpByStation(criteria);
			}
			if(result>0){
				resultVo.setStatus(1);
			}else{
				log.warn("configFpsByStation fail,params="+criteria.toString());
				resultVo.setStatus(-1);
			}
			voList.add(resultVo);
		}
		PageTO page = new PageTO();
		page.setDataList(voList);
		return returnResult(CODE_OK, STRING_OK, page);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
	public String updateFp(List<FpCriteria> criteriaList) throws Exception {
		for(FpCriteria criteria:criteriaList){
			Object o = redisManager.get(String.format(FP_STATUS, criteria.getFpId()));
			if(o == null){
				hqStationMapper.updateFpMonitor(criteria);
			}else{
				FpCriteria fpTO = gson.fromJson(String.valueOf(o), new TypeToken<FpCriteria>(){}.getType());
				if(fpTO.getSalePrice().doubleValue() != criteria.getSalePrice().doubleValue()
						|| fpTO.getPrId() != fpTO.getPrId()){
					hqStationMapper.updateFpMonitor(criteria);
				}
			}
			redisManager.set(String.format(FP_STATUS, criteria.getFpId()), gson.toJson(criteria),180);
		}
		return RESPONSE_CODE_OK;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
	public String syncAdminGroupHistory(AdminGroupHistoryVO vo, String hqId, String stationId) throws Exception {
		hqStationMapper.deleteGroupPaidHistory(hqId, stationId, vo.getGroupNo());
		hqStationMapper.deleteGroupChargeHistory(hqId, stationId, vo.getGroupNo());
		hqStationMapper.deleteGroupOrderHistory(hqId, stationId, vo.getGroupNo());
		hqStationMapper.deleteGroupCardHistory(hqId, stationId, vo.getGroupNo());
		for(AdminTO to:vo.getPaidList()){
			hqStationMapper.insertGroupPaidHistory(to);
		}
		for(AdminTO to:vo.getChargeList()){
			hqStationMapper.insertGroupChargeHistory(to);
		}
		for(AdminGroupFpOrderHistoryTO to:vo.getOrderList()){
			hqStationMapper.insertGroupOrderHistory(to);
		}
		for(AdminGroupFpOrderHistoryTO to:vo.getCardList()){
			hqStationMapper.insertGroupCardHistory(to);
		}
		return RESPONSE_CODE_OK;
	}

	@Override
	public String getStationsByHqId(HqStationCriteria criteria) throws Exception {
		List<StationTO> list = hqStationMapper.getStationsByHqId(criteria);
		PageTO page = new PageTO();
		page.setPageNo(criteria.getPageNo());
		page.setPageSize(criteria.getPageSize());
		if(criteria.getPageSize()!=null){
			int total = hqStationMapper.getCntStationsByHqId(criteria);
			page.setPageCount(totalPage(total,criteria.getPageSize()));
			page.setTotal(total);
		}
		page.setDataList(list);
		return returnResult(CODE_OK, STRING_OK, page);
	}
}
